/**
 * 全站路由配置
 *
 * 建议:
 * 1. 代码中路由统一使用name属性跳转(不使用path属性)
 */
import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import http from '@/libs/httpRequest'
import { isURL } from '@/libs/validate'
import { clearLoginInfo } from '@/libs'

Vue.use(Router)

// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const _import = require('./import-' + process.env.NODE_ENV)

// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [
  {
    path: '/404',
    component: _import('common/404'),
    name: '404',
    meta: { title: '404未找到' }
  },
  {
    path: '/login',
    component: _import('common/login/login'),
    name: 'login',
    meta: { title: '登录' }
  },
  {
    path: '/live',
    component: _import('common/live/live'),
    name: 'live',
    meta: { title: '直播' }
  },
  {
    path: '/replay',
    component: _import('common/replay/replay'),
    name: 'replay',
    meta: { title: '点播' }
  }
]

// 主入口路由(需嵌套上左右整体布局) layou1
const mainRoutes = {
  path: '/',
  component: _import('layout1/main'),
  name: 'main',
  redirect: { name: 'login' },
  meta: { title: '主入口整体布局' },
  children: [
    {
      path: '/home',
      component: _import('common/home'),
      name: 'home',
      meta: { title: '首页' }
    },
    {
      path: '/evaluation',
      component: _import('bizs/evaluation'),
      name: 'evaluation',
      meta: { title: '教学评价' }
    },
    {
      path: '/course-details',
      component: _import('common/evaluation-common/course-details'),
      name: 'course-details',
      meta: { title: '课程详情' }
    },
    {
      path: '/resources',
      component: _import('bizs/resources'),
      name: 'resources',
      meta: { title: '更多资源' }
    },
    {
      path: '/case-training',
      component: _import('bizs/management/case-training'),
      name: 'case-training',
      meta: { title: '案例联席研练' }
    }
  ],
  beforeEnter(to, from, next) {
    let token = Vue.cookie.get('token')
    if (!token || !/\S/.test(token)) {
      clearLoginInfo()
      next()
    } else {
      if (store.state.user.hasGetInfo) {
        next()
      } else {
        store
          .dispatch('getUserInfo')
          .then(() => {
            // 拉取用户信息，通过用户权限和跳转的页面的name来判断是否有权限访问;access必须是一个数组，如：['super_admin'] ['super_admin', 'admin']
            next()
          })
          .catch(() => {
            clearLoginInfo()
            next()
          })
      }
    }
  }
}
// layout2
const layout2 = {
  path: '/layout2',
  component: _import('layout2/main'),
  name: 'layout2',
  meta: { title: '主入口整体布局' },
  children: [
    {
      path: '/information',
      component: _import('bizs/information'),
      name: 'information',
      meta: { title: '消息' }
    },
    {
      path: '/personal',
      component: _import('bizs/personal'),
      name: 'personal',
      meta: { title: '个人中心' }
    },
    {
      path: '/study',
      component: _import('bizs/study'),
      name: 'study',
      meta: { title: '学习辅助' }
    }
    // {
    //   path: '/management',
    //   component: _import('bizs/management/management'),
    //   name: 'management',
    //   meta: { title: '用户管理' }
    // }
  ],
  beforeEnter(to, from, next) {
    let token = Vue.cookie.get('token')
    if (!token || !/\S/.test(token)) {
      clearLoginInfo()
      next()
    } else {
      if (store.state.user.hasGetInfo) {
        next()
      } else {
        store
          .dispatch('getUserInfo')
          .then(() => {
            // 拉取用户信息，通过用户权限和跳转的页面的name来判断是否有权限访问;access必须是一个数组，如：['super_admin'] ['super_admin', 'admin']
            next()
          })
          .catch(() => {
            clearLoginInfo()
            next()
          })
      }
    }
  }
}
// layout3
const layout3 = {
  path: '/layout3',
  component: _import('layout3/main'),
  name: 'layout3',
  meta: { title: '主入口整体布局' },
  children: [
    {
      path: '/user-management',
      component: _import('bizs/management/user-management'),
      name: 'user-management',
      meta: { title: '用户管理' }
    },
    {
      path: '/questionnaire-management',
      component: _import('bizs/management/questionnaire-management'),
      name: 'questionnaire-management',
      meta: { title: '问卷管理' }
    },
    {
      path: '/vocabulary-management',
      component: _import('bizs/management/vocabulary-management'),
      name: 'vocabulary-management',
      meta: { title: '高频词汇管理' }
    },
    {
      path: '/live-management',
      component: _import('bizs/management/live-management'),
      name: 'live-management',
      meta: { title: '教学直播成果管理' }
    },
    {
      path: '/case-management',
      component: _import('bizs/management/case-management'),
      name: 'case-management',
      meta: { title: '案列联席研练成果管理' }
    },
    {
      path: '/evaluation-management',
      component: _import('bizs/management/evaluation-management'),
      name: 'evaluation-management',
      meta: { title: '教学评价管理' }
    },
    {
      path: '/management-course-details',
      component: _import('common/evaluation-common/course-details'),
      name: 'management-course-details',
      meta: { title: '教学评价管理' }
    },
    {
      path: '/management-rating-item',
      component: _import('bizs/management/rating-item'),
      name: 'management-rating-item',
      meta: { title: '评分项管理' }
    },
    {
      path: '/live-start',
      component: _import('bizs/management/live-start'),
      name: 'live-start',
      meta: { title: '教学直播开课' }
    },
    {
      path: '/live-start-steps',
      component: _import('bizs/management/live-start-steps'),
      name: 'live-start-steps',
      meta: { title: '教学直播开课' }
    },
    {
      path: '/case-start',
      component: _import('bizs/management/case-start'),
      name: 'case-start',
      meta: { title: '案例联席研练开课' }
    },
    {
      path: '/case-start-steps',
      component: _import('bizs/management/case-start-steps'),
      name: 'case-start-steps',
      meta: { title: '案例联席研练开课' }
    }
  ],
  beforeEnter(to, from, next) {
    let token = Vue.cookie.get('token')
    if (!token || !/\S/.test(token)) {
      clearLoginInfo()
      next()
    } else {
      if (store.state.user.hasGetInfo) {
        next()
      } else {
        store
          .dispatch('getUserInfo')
          .then(() => {
            // 拉取用户信息，通过用户权限和跳转的页面的name来判断是否有权限访问;access必须是一个数组，如：['super_admin'] ['super_admin', 'admin']
            next()
          })
          .catch(() => {
            clearLoginInfo()
            next()
          })
      }
    }
  }
}

const router = new Router({
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  routes: globalRoutes
    .concat(mainRoutes)
    .concat(layout2)
    .concat(layout3)
})

export default router
